from django.shortcuts import render,HttpResponse,redirect
from django.db import connection
from django.urls import reverse
from django.http import FileResponse
import math,os
from django.conf import settings
from .models import Student
# Create your views here.

def index(request):
    data = Student.objects.all()
    return render(request, 'list.html', {'data': data})

def show(request):
    if not request.session.get('username'):
        return render(request,'login.html')
    with connection.cursor() as cursor:
        cursor.execute('select * from users left join class on users.class_id=class.id')
        data=cursor.fetchall()
    print(data)
    return render(request,'list.html',{'stus':data})
def delstu(request, id):
    with connection.cursor() as cursor:
        result=cursor.execute(f'delete from users where id={id}')
        # print(result)
    return redirect(reverse('myapp:myapp_list'))

def addstu(request):
    if request.method=="POST":
        name=request.POST.get('name')
        sex=request.POST.get('sex')
        phone=request.POST.get('phone')
        address=request.POST.get('address')
        status=request.POST.get('status')
        with connection.cursor() as cursor:
            cursor.execute(f'insert into users values(NULL,"{name}",{sex},{phone},{address},{status})')

        return redirect(reverse('myapp:myapp_list'))
    classed=getclass()
    return render(request,'addstu.html',{'class':classed})
def getclass():
    with connection.cursor() as cursor:
        cursor.execute('select * from class')
        classed=cursor.fetchall()
    return classed
def updatestu(request,id):
    if request.method=="POST":
        name = request.POST.get('name')
        sex = request.POST.get('sex')
        phone = request.POST.get('phone')
        address = request.POST.get('address')
        status = request.POST.get('status')
        with connection.cursor() as cursor:
            cursor.execute(f'update users set name="{name}",sex={sex},phone={phone},address={address},status={status} where id={id}')

        return redirect(reverse('myapp:myapp_list'))

    with connection.cursor() as cursor:
        cursor.execute(f'select * from users where id={id}')
        student=cursor.fetchone()
        print(student)
    classet=getclass()
    return render(request,'updatestu.html',{'stu':student,'class':classet})
#
#
# def admin_login(request):
#     if request.method=='POST':
#         username=request.POST.get('username')
#         password=request.POST.get('password')
#         with connection.cursor() as cursor:
#             cursor.execute(f'select * from admin_user where username="{username}" and password="{password}"')
#             user=cursor.fetchone()
#         if user is not None:
#             request.session['username']=username
#             return redirect(reverse('myapp:myapp_list'))
#         else:
#             return HttpResponse('账号或密码错误')
#
#     return render(request,'login.html')
#
# def set_cookie(request):
#     #假设登录成功！
#     # rep=HttpResponse('设置cookie')
#     # #10秒之后cookie失效
#     # rep.set_cookie('name','123',max_age=10)
#     # return rep
#     request.session['username']='zs'
#     return HttpResponse('设置session')
#
# def get_cookie(request):
#     #获取cookie
#     # cookie=request.COOKIES.get('name')
#     # return HttpResponse(cookie)
#
#     return HttpResponse(request.session['username'])




